package com.apes.pi.parser;

import com.apes.framework.api.ApesParser;
import com.apes.framework.plugin.esb.processor.ChainProcessor;
import com.apes.framework.plugin.esb.processor.ProducerProcessor;
import com.apes.framework.plugin.esb.processor.SampleChainProcessor;
import com.apes.pi.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能：PiQueue解析器
 *
 * @author xul
 * @create 2019-11-05 9:27
 */
@Component
public class QueueParser implements ApesParser<PiQueue, ChainProcessor> {

    @Autowired
    private QueueCacheManager cacheManager;

    @Autowired
    private PipeParser pipeParser;

    @Autowired
    private PiConnectorParser connectorParser;

    @Override
    public ChainProcessor parse(PiQueue queue) {
        ChainProcessor processor = new SampleChainProcessor();

        /**前置拦截：队列**/
        setPipeProcessor(processor, queue.getId(), "before", "Queue", new HashMap<>());

        List<QueueReceiver> queueReceivers = cacheManager.getQueueReceiversByQueueId(queue.getId());
        if (queue.getType() == 0) { /**传输模式（0:同步，1:异步）**/
            for (QueueReceiver queueReceiver : queueReceivers) {
                setQueueReceiverProcessor(processor, queueReceiver);
            }
            /**后置拦截：队列**/
            setPipeProcessor(processor, queue.getId(), "after", "Queue", new HashMap<>());
        } else {
            for (QueueReceiver queueReceiver : queueReceivers) {
                ProducerProcessor producerProcessor = new ProducerProcessor();
                producerProcessor.setTopic(queueReceiver.topicName());
                producerProcessor.setDescription(String.format("%s:%s", queue.getName(), queueReceiver.getReceiver().getName()));
                processor.setProcessor(producerProcessor);
            }
        }
        return processor;
    }

    public void setQueueReceiverProcessor(ChainProcessor processor, QueueReceiver queueReceiver) {
        Receiver receiver = cacheManager.getReceivers().get(queueReceiver.getReceiver().getId());
        Map<String, String> params = new HashMap<>();
        receiver.getParams().stream().forEach(param -> params.put(param.getType(), param.getValue()));
        /**前置拦截：队列接收者**/
        setPipeProcessor(processor, queueReceiver.getId(), "before", "QueueReceiver", params);
        /**前置拦截：接收者**/
        setPipeProcessor(processor, queueReceiver.getReceiver().getId(), "before", "Receiver", params);

        /**传输**/
        PiConnector piConnector = cacheManager.getPiConnectors().get(receiver.getConnector().getId());
        setConnectorProcessor(processor, piConnector);

        /**后置拦截：接收者**/
        setPipeProcessor(processor, queueReceiver.getReceiver().getId(), "after", "Receiver", params);
        /**后置拦截：队列接收者**/
        setPipeProcessor(processor, queueReceiver.getId(), "after", "QueueReceiver", params);
    }

    public void setPipeProcessor(ChainProcessor processor, String parentId, String position,
                                 String parentType, Map<String, String> params) {
        List<Pipe> pipes = cacheManager.getPipeByParentId(parentId, position, parentType);
        for (Pipe pipe : pipes) {
            PipeContext context = new PipeContext(params, pipe);
            ChainProcessor pipeProcessor = pipeParser.parse(context);
            processor.setProcessor(pipeProcessor);
        }
    }

    public void setConnectorProcessor(ChainProcessor processor, PiConnector piConnector) {
        ChainProcessor connector = connectorParser.parse(piConnector);
        if (connector == null) return;
        processor.setProcessor(connector);
    }

}
